﻿
@{
    ViewBag.Title = "生产报表";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay > new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay > new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : DateTime.Today.AddDays(-1).AddHours(8));
    var endTime = DateTime.Now.TimeOfDay > new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : (DateTime.Now.TimeOfDay > new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddDays(-1).AddHours(20));
}

@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" name="workShopCode" v-html="lang.Field.WorkShop">生产车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" name="workShopCode" v-html="lang.Field.AssemblyLine">流水线：</label>
                        <div class="layui-input-inline">
                            <select id="selLines">
                                <option value="0">全部</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ProduceTime">生产时间：</label>
                        <div class="layui-input-inline" style="width:440px;">
                            <input type="text" id="txtRangeTime" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-top:-6px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button" style="margin-top:-6px;margin-left:0;">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button" style="margin-top:-6px;">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnExport" target="_blank" class="layui-btn layui-btn-normal layuiadmin-btn-list" style="margin-top:-6px;" href="javascript:void(0)">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">过站统计</div>
            <div class="layui-card-body">
                <table id="tableViaSite" lay-filter="tableViaSite"></table>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">碎片率统计</div>
            <div class="layui-card-body">
                <table id="tableCompenRate" lay-filter="tableCompenRate"></table>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">合格率统计</div>
            <div class="layui-card-body">
                <table id="tablePassRate" lay-filter="tablePassRate"></table>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">IV分布</div>
            <div class="layui-card-body">
                <table id="tableIv" lay-filter="tableIv"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , range: '至'
                    , done: function (value, date, endDate) {
                        queryNos(0);
                        queryNos(1);
                    }
                });
                form.on('select(selWorkshops)', function (data) {
                    console.log(data.value); //得到被选中的值
                    queryLines();
                    queryNos(0);
                    queryNos(1);
                });

                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");

                queryNos(0);
                queryNos(1);
                queryLines();
                function queryLines() {
                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/QueryLines",
                        data: {
                            workshopCode: $("#selWorkshops").val().trim()
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value='0'>全部</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i].PkId + "'>" + res.Data[i].LineName + "</option>");
                            }
                            $("#selLines").html(html.join(""));
                            form.render("select");
                        }
                    });
                }
                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }

                var viaSiteOptions = {
                    elem: '#tableViaSite'
                    , id: 'tableViaSite'
                    , url: '/Report/QueryViaSiteStatistics'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, orderNo: "", workOrderNo: "", startTime: "@(startTime.ToString("yyyy-MM-dd HH:mm:ss"))", endTime: "@(endTime.ToString("yyyy-MM-dd HH:mm:ss"))" }
                    , page: false
                    , limit: 100
                    , even: true
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'SiteType', title: '工序' }
                        , { field: 'QLevel', title: '等级' }
                        , { field: 'Count', title: '产量' }
                    ]]
                };
                var compenOptions = {
                    elem: '#tableCompenRate'
                    , id: 'tableCompenRate'
                    , url: '/Report/QueryCompenRates'
                    , method: "post"
                    , where: { workshopCode: "", orderNo: "", workOrderNo: "", startTime: "@(startTime.ToString("yyyy-MM-dd HH:mm:ss"))", endTime: "@(endTime.ToString("yyyy-MM-dd HH:mm:ss"))" }
                    , page: false
                    , limit: 100
                    , even: true
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'CompenReason', title: '统计类别' }
                        , { field: 'Molecular', title: '分子' }
                        , { field: 'Denominator1', title: '分母1' }
                        , { field: 'Denominator2', title: '分母2' }
                        , { field: 'Rate1', title: '比率1(%)' }
                        , { field: 'Rate2', title: '比率2(%)' }
                    ]]
                };
                var passRateOptions = {
                    elem: '#tablePassRate'
                    , id: 'tablePassRate'
                    , url: '/Report/QueryPassRates'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, orderNo: "", workOrderNo: "", startTime: "@(startTime.ToString("yyyy-MM-dd HH:mm:ss"))", endTime: "@(endTime.ToString("yyyy-MM-dd HH:mm:ss"))" }
                    , page: false
                    , limit: 100
                    , even: true
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'Category', title: '统计类别' }
                        , { field: 'Molecular', title: '分子' }
                        , { field: 'Denominator', title: '分母' }
                        , { field: 'Rate', title: '比率(%)' }
                        , { field: 'Remark', title: '备注' }
                    ]]
                };
                var ivOptions = {
                    elem: '#tableIv'
                    , id: 'tableIv'
                    , url: '/Report/QueryIvDistributes'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, orderNo: "", workOrderNo: "", startTime: "@(startTime.ToString("yyyy-MM-dd HH:mm:ss"))", endTime: "@(endTime.ToString("yyyy-MM-dd HH:mm:ss"))" }
                    , page: false
                    , limit: 100
                    , even: true
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'Category', title: '统计类别' }
                        , { field: 'Molecular', title: '分子' }
                        , { field: 'Denominator', title: '分母' }
                        , { field: 'Rate', title: '比率(%)' }
                        , { field: 'Remark', title: '备注' }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable(false);
                });
                queryTable(true);
                function queryTable(fstTime) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    var endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    var where = {
                        workshopCode: $("#selWorkshops").val().trim(),
                        lineId: parseInt($("#selLines").val().trim()),
                        orderNo: $("#txtOrderNo").val().trim(),
                        workOrderNo: $("#txtWorkOrderNo").val().trim(),
                        startTime: startTime,
                        endTime: endTime
                    };
                    viaSiteOptions.where = where;
                    compenOptions.where = where;
                    passRateOptions.where = where;
                    ivOptions.where = where;

                    $("#btnExport").attr("href", "/Report/ExportProductionReports?workshopCode=" + where.workshopCode + "&lineId=" + where.lineId + "&orderNo=" + where.orderNo + "&workOrderNo=" + where.workOrderNo + "&startTime=" + startTime + "&endTime=" + endTime);

                    if (fstTime) {
                        table.render(viaSiteOptions);
                        table.render(compenOptions);
                        table.render(passRateOptions);
                        table.render(ivOptions);
                    } else {
                        table.reload("tableViaSite", viaSiteOptions);
                        table.reload("tableCompenRate", compenOptions);
                        table.reload("tablePassRate", passRateOptions);
                        table.reload("tableIv", ivOptions);
                    }
                }
            });
        }

        function detail(barCode) {
            parent.layui.index.openTabsPage("/Report/ComponentDetail?barCode=" + barCode, "组件详情(" + barCode + ")");
        }
    </script>
}
